package com.gimi.cloud.mmf.gateway.service.impl;

import com.gimi.cloud.mmf.gateway.common.enums.ResponseCodeEnum;
import com.gimi.cloud.mmf.gateway.common.jwt.Jwt;
import com.gimi.cloud.mmf.gateway.common.reqhead.Response;
import com.gimi.cloud.mmf.gateway.service.CheckTokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by luck on 2017/10/11.
 */
@Service
public class CheckTokenServiceImpl  implements CheckTokenService {
    private static Logger logger = LoggerFactory.getLogger(CheckTokenServiceImpl.class);


    @Override
    public Response checkToken(String token, String secret) {
        logger.info("进入token验证");
        Response resp = null;
        //logger.info(userTestDao.selectAll().toString());
        Map<String, Object> resultMap = Jwt.validToken(token,secret);
        String state = resultMap.get("state") != null ? resultMap.get("state").toString() : "";
        switch (state){
            case "VALID":
                break;
            case "INVALID":
                resp = new Response();
                resp.setResponseCode(ResponseCodeEnum.SIGNATURE_ERROR);
                break;
            case "EXPIRED":
                resp = new Response();
                resp.setResponseCode(ResponseCodeEnum.SIGNATURE_TIMEOUT,"令牌已过期");
                break;
            default:
                resp = new Response();
                resp.setResponseCode(ResponseCodeEnum.SIGNATURE_ERROR,"校验失败");
                break;
        }
        logger.info("token验证结束.");
        return resp;
    }
}
